﻿using Autofac;
using DevExpress.Skins;
using DevExpress.UserSkins;
using EyFramework.Common;
using EyFramework.Library;
using EyFramework.Sys.Core;
using EyFramework.Sys.Core.Service.Impl;
using System;
using System.Threading;
using System.Windows.Forms;
using EyFramework.Common.Entity;

//using EyFramework.Sys.Core;
//using EyFramework.Sys.Core.Service.impl;

namespace EyFramework.Main
{
    /// <summary>
    /// 启动Dev程序入口
    /// </summary>
    public static class StartDevApp
    {
        /// <summary>
        ///  启动App
        /// </summary>
        /// <returns>True 启动成功，False 启动失败</returns>
        public static bool RunApp(string[] args)
        {
            StartUpArgs startUpArgs = Globals.GetStartUpArgs(args);
            BonusSkins.Register();
            SkinManager.EnableFormSkins();//启用窗体支持换肤特性
            DevExpress.XtraEditors.Controls.Localizer.Active = new MessboxClass();
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);//捕获系统所产生的异常。
            //Dev环境，需要初始化的对象。因为程序使用的时接口。需要指定实现类，否则会抛出空指针异常
            RegisterSysModule();
            Globals.MsgUtil = new DevMsgUtil();
            Globals.ExceptionHandler = new DBExceptionHandler();
            AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnNewAssemblyLoaded);
            if (FrmLogin.Login(startUpArgs))
            {
                //设置全局主窗体
                FormGlobals.MainForm.ShowDialog();
                return true;
            }
            return false;
        }

        /// <summary>
        /// AutoFac手工注册Sys模块
        /// </summary>
        private static void RegisterSysModule()
        {
            if (Globals.container == null)
            {
                var builder = new ContainerBuilder();
                builder.RegisterModule(new ModuleSystem());
                Globals.container = builder.Build();
            }
        }

        /// <summary>
        /// AutoFac自动注册各模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void OnNewAssemblyLoaded(object sender, AssemblyLoadEventArgs args)
        {
            if (!Globals.assemblyList.Contains(args.LoadedAssembly.FullName))
            {
                Globals.assemblyList.Add(args.LoadedAssembly.FullName);
                if (Globals.container == null)
                {
                    var builder = new ContainerBuilder();
                    builder.RegisterAssemblyModules(args.LoadedAssembly);
                    Globals.container = builder.Build();
                }
                else
                {
                    var builder = new ContainerBuilder();
                    builder.RegisterAssemblyModules(args.LoadedAssembly);
                    builder.Update(Globals.container);
                }
            }
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Globals.ShowException(e.Exception);
        }

        /// <summary>
        /// 启动自动任务APP
        /// </summary>
        /// <param name="args"></param>
        public static void RunTaskApp(string[] args)
        {
            BonusSkins.Register();
            SkinManager.EnableFormSkins();//启用窗体支持换肤特性
            DevExpress.XtraEditors.Controls.Localizer.Active = new MessboxClass();
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);//捕获系统所产生的异常。
            //Dev环境，需要初始化的对象。因为程序使用的时接口。需要指定实现类，否则会抛出空指针异常
            RegisterSysModule();
            Globals.MsgUtil = new DevMsgUtil();
            Globals.ExceptionHandler = new DBExceptionHandler();
            AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnNewAssemblyLoaded);
        }
    }
}